package com.api.rocky.app;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.api.rocky.R;
import com.api.rocky.Data.Global;
import com.api.rocky.Util.Util;
import com.api.rocky.base.LoveWordAdapter;
import com.api.rocky.base.MessageAdapter;

public class LoveWordListActivity extends Activity {
	private LoveWordAdapter loveWordAdapter;
	private Button but_near,but_city,but_hot;
	private ListView lv_loveWord;
	
	private List<Map<String, Object>> mData; 
	
	private Context loveWordListActivity;
	
	LoveWordListReceiver loveWordListReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_love_word_list);
        
        but_near = (Button) findViewById(R.id.loveword_near);
        but_city = (Button) findViewById(R.id.loveword_city);
        but_hot = (Button) findViewById(R.id.loveword_hot);
        
        but_near.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View arg0) {
        		requestNear();
        	}
        });
        but_city.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View arg0) {
        		requestCity();
        	}
        });
        
        lv_loveWord = (ListView)findViewById(R.id.list_loveword);
        loveWordListReceiver = new LoveWordListReceiver();
        loveWordListActivity = this;
    }
    
    
    @Override
	protected void onStart() {
		super.onStart();
		IntentFilter filter = new IntentFilter();
		filter.addAction("loveword");
		loveWordListActivity.registerReceiver(loveWordListReceiver, filter);
		requestNear();
	}


    

	@Override
	protected void onDestroy() {
		super.onDestroy();
		/*loveWordListActivity.unregisterReceiver(loveWordListReceiver);*/
	}



	@Override
	protected void onResume() {
		super.onResume();
		//requestNear();
	}
	
	private void requestCity(){
		try{
			while(Global.LOCATION_X == 0 && Global.LOCATION_Y == 0){
				Thread.sleep(500);
			}
			JSONArray jsa = new JSONArray();
			jsa.put(8);
			jsa.put(1);
			jsa.put(Global.LOCATION_CITY);
			Util.SendMessageToServer(Global.client, jsa.toString());
		}catch(Exception e){
			Log.e("love word list.", e.toString());
		}
	}
	
	private void requestNear(){
		try{
			while(Global.LOCATION_X == 0 && Global.LOCATION_Y == 0){
				Thread.sleep(500);
			}
			JSONArray jsa = new JSONArray();
			jsa.put(8);
			jsa.put(0);
			jsa.put(Global.LOCATION_X);
			jsa.put(Global.LOCATION_Y);
			Util.SendMessageToServer(Global.client, jsa.toString());
		}catch(Exception e){
			Log.e("love word list.", e.toString());
		}
	}

    
	private void setLoveWordData(JSONArray jsa) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Calendar cal = Calendar.getInstance();
		int thisYear = cal.get(Calendar.YEAR);
        for(int i = 0 ; i < jsa.length() ; i++){
        	try{
	        	JSONObject jso = jsa.getJSONObject(i);
		        Map<String, Object> map = new HashMap<String, Object>(); 
		        map.put("face", R.drawable.gril003);
		        map.put("name", jso.getString("nickname"));
		        map.put("sex", (jso.getInt("sex") == 0?R.drawable.male:R.drawable.female));
		        map.put("age", thisYear - jso.getInt("birthday"));
		        map.put("time", jso.getString("time"));
		        map.put("message", jso.getString("message"));
		        list.add(map);
        	}catch(Exception e){
        		Log.e("Love word list", e.toString());
        	}
        }
        mData = list;
        loveWordAdapter = new LoveWordAdapter(loveWordListActivity,mData);
        lv_loveWord.setAdapter(loveWordAdapter);
    }

	
    class LoveWordListReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			String action = arg1.getAction();
			if(action.equals("loveword")){
				JSONArray jsa;
				try {
					jsa = new JSONArray(arg1.getStringExtra("loveword"));
					setLoveWordData(jsa);
					loveWordAdapter.notifyDataSetChanged();
				} catch (JSONException e) {
					Log.e("LoveWordListReceiver", e.toString());
				}
			}
			
		}  
    	
    }
}
